<?php

namespace app\command\install;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Db;

/**
 * 安装cms,主要操作表
 */
class Install extends Command
{
    protected function configure() {
        // 指令配置
        $this->setName('install')
            ->setDescription('cms install')
            ->setHelp(sprintf('%scms install %s', PHP_EOL, PHP_EOL));
    }

    protected function execute(Input $input, Output $output)
    {
        $this->_execSql($output);

        // 用程序注册后台管理员
        //INSERT INTO `s_system_accounts` VALUES (1, 1675914754, 'admin', '19862265752', '$2y$10$11kH3gEoZw28RiTy/VtXnOcCLoYBaoggAMKnhGTnPwOE7LXctnrUG', 'd90f7061e4b6a6b34265d05c769a7116', 1);
    }

    private function _execSql(Output $output) {
        $prefix = config('database.connections.mysql.prefix');

        // 后台管理员表
        Db::execute("DROP TABLE IF EXISTS `{$prefix}system_accounts`");
        Db::execute("CREATE TABLE `{$prefix}system_accounts`  (
                                     `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'ID',
                                     `lastlogin` int(11) NOT NULL DEFAULT 0 COMMENT '最后登录',
                                     `username` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '姓名',
                                     `phone` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '手机号',
                                     `password` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '密码',
                                     `token` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT 'token.用于自动登录',
                                     `status` int(11) NOT NULL DEFAULT 0 COMMENT '状态',
                                     `updated_at` timestamp NULL DEFAULT NULL COMMENT '修改时间'
                                     PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '后台管理员表' ROW_FORMAT = Dynamic;");

        $password = randStr(6);
        $data = [
            'id' => 1,
            'lastlogin' => 0,
            'username'  => 'admin',
            'phone'     => '111',
            'password'  => password_hash($password, PASSWORD_BCRYPT),
            'token'     => '',
            'status'    => 1,
        ];
        Db::name('system_accounts')->insert($data);
        $output->writeln('password: ' . $password);

        // 管理员权限表
        Db::execute("DROP TABLE IF EXISTS `{$prefix}system_account_auths`;");
        Db::execute("CREATE TABLE `{$prefix}system_account_auths`  (
                                          `account_id` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '用户编号',
                                          `menu_id` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '内容',
                                          `update_time` int(11) DEFAULT 0 COMMENT '更新时间',
                                          PRIMARY KEY (`account_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '用户菜单权限管理' ROW_FORMAT = Dynamic;");

        // 图标表
        Db::execute("DROP TABLE IF EXISTS `{$prefix}system_icons`;");
        Db::execute("CREATE TABLE `{$prefix}system_icons`  (
                                  `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
                                  `icon` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '图标',
                                  `desc` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '说明',
                                  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '图标表' ROW_FORMAT = Dynamic;
");

        // 菜单表
        Db::execute("DROP TABLE IF EXISTS `{$prefix}system_menus`;");
        Db::execute("CREATE TABLE `{$prefix}system_menus`  (
                                  `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
                                  `pid` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '上级id',
                                  `target` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '跳转target',
                                  `name` varchar(60) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '功能名称',
                                  `url` varchar(60) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT 'url',
                                  `sort` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '排序',
                                  `icon` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '图标',
                                  `status` tinyint(4) NOT NULL DEFAULT 1 COMMENT '状态0正常,1禁用',
                                  `update_id` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '更新管理员id',
                                  `updated_at` timestamp(0) NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP(0) COMMENT '更新时间',
                                  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '系统菜单表' ROW_FORMAT = Dynamic;
");

        // 用户表
        Db::execute("DROP TABLE IF EXISTS `{$prefix}system_users`;");
        Db::execute("CREATE TABLE `{$prefix}system_users` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `phone` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '手机号',
  `nickname` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户名',
  `image` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户头像地址',
  `is_logoff` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否注销.0未注销.1注销',
  `create_time` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '注册时间',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='用户表';");

        // 登录token表.
        Db::execute("DROP TABLE IF EXISTS `{$prefix}system_user_tokens`;");
        Db::execute("CREATE TABLE `{$prefix}system_user_tokens` (
  `user_id` int(11) NOT NULL COMMENT '用户ID',
  `username` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户名',
  `token` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT 'token',
  `expiretime` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '过期时间',
  PRIMARY KEY (`user_id`) USING BTREE,
  KEY `usertoken_token_INDEX` (`token`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='用户登录token表';");

        // 登录token表.
        Db::execute("DROP TABLE IF EXISTS `{$prefix}system_user_codes`;");
        Db::execute("CREATE TABLE `{$prefix}system_user_codes` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `phone` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '手机号',
  `code` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT 'code',
  `used` tinyint(255) NOT NULL DEFAULT '0' COMMENT '是否使用.0未使用.1已使用',
  `create_time` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '创建时间',
  `used_time` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '使用时间',
  PRIMARY KEY (`id`),
  KEY `phone_index` (`phone`) USING BTREE COMMENT '手机号'
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='用户短信验证码';");

        // 插入数据.
        $sqlContent = file_get_contents(app()->getAppPath() . 'command/install/install.sql');
        $sqlList = explode(';', $sqlContent);
        foreach($sqlList as $sqlInfo) {
            if(!trim($sqlInfo)) continue;
            $sqlInfo = str_replace('prefix_', $prefix, $sqlInfo);
            Db::execute($sqlInfo);
        }
    }
}